﻿using System;
using System.Data;
using System.Linq;
using MvcDesign.Core.DataAccess;

namespace MvcDesign.Forum.ForumServices
{
    class DataInitializer : IDataInitializer
    {
        //static DataInitializer()
        //{
            //DataAccess.DataInitializer.RegisterInitializer(new DataInitializer());            
        //}

        public void Initialize(DataContext context)
        {
            var section1 = new ForumDataModel.Section
            {
                Name = "Common Information",
                Description = "Description of common information",
                IsForum = false,
            };

            context.GetCollection<ForumDataModel.Section>().Add(section1);

            context.SaveChanges();

            var testUser = context.GetCollection<Core.CoreDataModel.User>().First();

            if (!section1.Id.HasValue || !testUser.Id.HasValue)
            {
                throw new NoNullAllowedException();
            }

            var subSection1 = new ForumDataModel.Section
            {
                Name = "Common Section 1",
                Description = "Common Section Description",
                IsForum = true,
                ParentSectionId = section1.Id.Value
            };

            context.GetCollection<ForumDataModel.Section>().Add(subSection1);

            var subSection2 = new ForumDataModel.Section
            {
                Name = "Common Section 2",
                Description = "Description of Common Section 2",
                IsForum = true,
                ParentSectionId = section1.Id.Value
            };

            context.GetCollection<ForumDataModel.Section>().Add(subSection2);

            var section2 = new ForumDataModel.Section
            {
                Name = "Private Information",
                Description = "Closed section",
                IsForum = false,
            };

            context.GetCollection<ForumDataModel.Section>().Add(section2);

            context.SaveChanges();

            if (!section2.Id.HasValue)
            {
                throw new NoNullAllowedException();
            }

            var subSection3 = new ForumDataModel.Section
            {
                Name = "Private Section 1",
                Description = "Description of Private Section 1",
                IsForum = true,
                ParentSectionId = section2.Id.Value
            };

            context.GetCollection<ForumDataModel.Section>().Add(subSection3);

            context.SaveChanges();

            if (!subSection3.Id.HasValue)
            {
                throw new NoNullAllowedException();
            }

            var thread1 = new ForumDataModel.Thread
            {
                AuthorId = testUser.Id.Value,
                Created = DateTime.UtcNow,
                Description = "Thread description",
                Name = "New Thread",
                SectionId = subSection3.Id.Value
            };

            context.GetCollection<ForumDataModel.Thread>().Add(thread1);

            context.SaveChanges();

            if (!thread1.Id.HasValue)
            {
                throw new NoNullAllowedException();
            }

            context.GetCollection<ForumDataModel.Post>().Add(new ForumDataModel.Post
            {
                AuthorId = testUser.Id.Value,
                Created = DateTime.UtcNow,
                Title = "Post Title",
                Text = "Post Message",
                ThreadId = thread1.Id.Value,
            });
        }
    }
}
